package com.ys.service.sys.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.ys.entity.finance.TbCompanyBankInfoAccount;
import com.ys.mapper.finance.TbCompanyBankInfoAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.sys.TbCompanyBankInfoMapper;
import com.ys.service.sys.TbCompanyBankInfoService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;

@Service("tbCompanyBankInfoService")
public class TbCompanyBankInfoServiceImpl implements TbCompanyBankInfoService {

    @Autowired
    private TbCompanyBankInfoMapper tbCompanyBankInfoMapper;

    @Autowired
    private TbCompanyBankInfoAccountMapper tbCompanyBankInfoAccountMapper;

    @Autowired
    private CheckUtil checkUtil;


    @Override
    public R add(Map<String, Object> params) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TbCompanyBankInfo tbCompanyBankInfo = JSON.parseObject(JSON.toJSONString(params), TbCompanyBankInfo.class);

        tbCompanyBankInfoMapper.insert(tbCompanyBankInfo);
        //实时公司银行账户管理
        TbCompanyBankInfoAccount tbCompanyBankInfoAccount = new TbCompanyBankInfoAccount();
        tbCompanyBankInfoAccount.setCompanyBankInfoVid(tbCompanyBankInfo.getVid());
        tbCompanyBankInfoAccount.setRemark(tbCompanyBankInfo.getVbz());
        tbCompanyBankInfoAccount.setAccount(BigDecimal.ZERO);
        tbCompanyBankInfoAccount.setPid(tbCompanyBankInfo.getId());
        tbCompanyBankInfoAccount.setStatus(1);
        if(tbCompanyBankInfo.getStatus()==1) {
            int tbCompanyBankInfoAccount1 = new TbCompanyBankInfoAccount()
                    .setPid(tbCompanyBankInfo.getId())
                    .count();
            if(tbCompanyBankInfoAccount1==0) {
                tbCompanyBankInfoAccountMapper.insert(tbCompanyBankInfoAccount);
            }
        }
        return R.ok(AddDto.gotoEdit("/web/sys/tbCompanyBankInfo/toEdit?id=" + tbCompanyBankInfo.getId()));
    }

    /**
     * 修改公司银行信息
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TbCompanyBankInfo tbCompanyBankInfo = JSON.parseObject(JSON.toJSONString(params), TbCompanyBankInfo.class);

        tbCompanyBankInfoMapper.update(tbCompanyBankInfo);

        TbCompanyBankInfoAccount tbCompanyBankInfoAccount2 = new TbCompanyBankInfoAccount()
                .setPid(tbCompanyBankInfo.getId())
                .queryFirst();
        tbCompanyBankInfoAccount2.setStatus(tbCompanyBankInfo.getStatus());
        tbCompanyBankInfoAccountMapper.update(tbCompanyBankInfoAccount2);

        return R.ok(AddDto.gotoEdit("/web/sys/tbCompanyBankInfo/toEdit?id=" + tbCompanyBankInfo.getId()));

    }

    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+10249L)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TB_CompanyBankInfo",
                    listField,
                    infoMap,
                    ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
